<!-- 自定义标准图表 设置百分百宽度 通过平台组件来适配尺寸 可以替换数据以及设置高级属性自定义扩展 详细查看组件示例.txt
  注:如需设置图片存放文件夹名称需应与当前文件名一致(1并在aircim.webpack.build.component.config.js配置需要打包的文件 2.npm run lib_c 执行打包命令  最后的组件会打包在components/grids中)-->
<template>
  <div class="scale-box" :id="dynamicIdName()">
    <div
      class="hall-pj-box animate__animated"
      :class="dynamicClassName"
      style="width: 880px;height: 260px;"
      :id="dynamicChildName()"
    >
      <div class="center_title">评价分析</div>
      <div class="hall-stars">
        <div class="hall-stars-item">
          <p class="stars"><i></i></p>
          <p class="name">五星评价</p>
          <p class="num"><em>2135</em>个</p>
        </div>
        <div class="hall-stars-item">
          <p class="stars"><i style="width: 80%;"></i></p>
          <p class="name">四星评价</p>
          <p class="num"><em>2135</em>个</p>
        </div>
        <div class="hall-stars-item">
          <p class="stars"><i style="width: 60%;"></i></p>
          <p class="name">三星评价</p>
          <p class="num"><em>2135</em>个</p>
        </div>
        <div class="hall-stars-item">
          <p class="stars"><i style="width: 40%;"></i></p>
          <p class="name">二星评价</p>
          <p class="num"><em>2135</em>个</p>
        </div>
        <div class="hall-stars-item">
          <p class="stars"><i style="width: 20%;"></i></p>
          <p class="name">一星评价</p>
          <p class="num"><em>2135</em>个</p>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
//引入封装方法
import encapsulation from "../../utils/aircimGird";
export default {
  //混入封装方法
  mixins: [encapsulation],
  data() {
    return {
      resizeObserver: null,
      //动态Id
      timestamp: null,
    };
  },
  mounted() {
    this.timestamp = new Date().getTime();
    this.$nextTick(() => {
      // 监听盒子变化
      this.listenChangeFn();
    });
  },
  methods: {
    //监听外部盒子的大小 然后对数据井行缩放
    scaleFn() {
      const parentBox = document.getElementById(`dynamic-${this.timestamp}`);
      const childBox = document.getElementById(
        `dynamicChild-${this.timestamp}`
      );
      const parentWidth = parentBox.offsetWidth;
      const parentHeight = parentBox.offsetHeight;
      const childWidth = childBox.offsetWidth;
      const childHeight = childBox.offsetHeight;
      let scaleX, scaleY;
      if (parentWidth < childWidth || parentHeight < childHeight) {
        const scaleBasedOnWidth = parentWidth / childWidth;
        const scaleBasedOnHeight = parentHeight / childHeight;
        scaleX = scaleBasedOnWidth;
        scaleY = scaleBasedOnHeight;
      } else {
        scaleX = parentWidth / childWidth;
        scaleY = parentHeight / childHeight;
      }
      childBox.style.transform = `scale(${scaleX}, ${scaleY})`;
    },
    // 监听盒子变化
    listenChangeFn() {
      let that = this;
      const element = document.getElementById(`dynamic-${this.timestamp}`);
      // console.log(element, "element");
      if (element) {
        new ResizeObserver(() => {
          // 处理元素大小变化的逻辑
          that.scaleFn();
        }).observe(element);
      }
    },
    // 绑定动画样式 animate__flash
    bindAnimationFn(animateName) {
      if (animateName != null) {
        this.dynamicClassName = animateName;
      }
    },
    //定义唯一ID名
    dynamicIdName() {
      return `dynamic-${this.timestamp}`;
    },
    //定义唯一ID名
    dynamicChildName() {
      return `dynamicChild-${this.timestamp}`;
    },
  },
  // 销毁监听
  beforeDestroy() {
    // 组件销毁前停止监听
    if (this.resizeObserver) {
      this.resizeObserver.disconnect();
    }
    if (this.requestTime != null) {
      clearInterval(this.requestTime);
    }
  },
};
</script>
<style lang="scss" scoped>
@import "../../static/assets/common.scss";
</style>
<style lang="scss" scoped>
.scale-box {
  width: 100%;
  height: 100%;
}

.hall-pj-box {
  width: 880px;
  height: 260px;
  position: absolute;
  padding: 0 20px;
  box-sizing: border-box;
  overflow: hidden;
  transform-origin: left top;
  // background-image: url(../../../thumbnail/backImg/new-border-green.png);
  // background-size: 100% 100%;
  // background-repeat: no-repeat;

  .grid-charts {
    width: 100%;
    height: 100%;
  }
}

.hall-stars {
  display: flex;
  text-align: center;
  justify-content: space-between;

  .hall-stars-item {
    background-image: url(../../static/assets/HallPj/itembg.png);
    background-repeat: no-repeat;
    background-position: center center;
    height: 150px;

    &:nth-child(2n) {
      margin-top: 59px;
    }
  }

  .stars {
    margin: 0 auto;
    width: 124px;
    height: 20px;

    i {
      margin: 0 auto;
      display: block;
      height: 100%;
      background: url(../../static/assets/HallPj/star.png) no-repeat left center;
    }
  }

  .name {
    margin-bottom: 70px;
    margin-top: 6px;
    line-height: 27px;
    color: #fff;
    font-size: 18px;
  }

  .num {
    em {
      color: #ffbd3c;
      font-size: 24px;
    }
  }
}

.center_title {
  margin-bottom: 20px;
  display: flex;
  color: #fff;
  font-size: 24px;
  font-weight: 700;
  gap: 12px;
  align-items: center;
  justify-content: center;

  &::after,
  &::before {
    width: 175px;
    height: 15px;
    content: "";
    display: block;
    background-image: url(../../static/assets/HallPj/title.png);
  }

  &::before {
    transform: rotate(180deg);
  }
}
</style>
